import 'dart:io';

class MediaFile {
  final int? id;
  final File file;
  final String name;
  final String path;
  final int size;
  final DateTime lastModified;
  final DateTime? createdAt;
  final DateTime? scannedAt;
  final MediaType type;
  final String extension;
  
  // 照片元数据
  final String? cameraMake;
  final String? cameraModel;
  final String? lensMake;
  final String? lensModel;
  final double? focalLength;
  final double? aperture;
  final int? iso;
  final String? shutterSpeed;
  final int? width;
  final int? height;
  final double? gpsLatitude;
  final double? gpsLongitude;
  final DateTime? dateTaken;
  
  // 视频元数据
  final double? durationSeconds;
  final double? frameRate;
  final int? bitrate;
  final String? codec;

  MediaFile({
    this.id,
    required this.file,
    required this.name,
    required this.path,
    required this.size,
    required this.lastModified,
    this.createdAt,
    this.scannedAt,
    required this.type,
    required this.extension,
    
    // 照片元数据
    this.cameraMake,
    this.cameraModel,
    this.lensMake,
    this.lensModel,
    this.focalLength,
    this.aperture,
    this.iso,
    this.shutterSpeed,
    this.width,
    this.height,
    this.gpsLatitude,
    this.gpsLongitude,
    this.dateTaken,
    
    // 视频元数据
    this.durationSeconds,
    this.frameRate,
    this.bitrate,
    this.codec,
  });

  // 从数据库Map创建MediaFile实例
  factory MediaFile.fromDatabase(Map<String, dynamic> data) {
    return MediaFile(
      id: data['id'],
      file: File(data['file_path']),
      name: data['file_name'],
      path: data['file_path'],
      size: data['file_size'],
      lastModified: DateTime.fromMillisecondsSinceEpoch(data['modified_at']),
      createdAt: data['created_at'] != null 
        ? DateTime.fromMillisecondsSinceEpoch(data['created_at'])
        : null,
      scannedAt: data['scanned_at'] != null 
        ? DateTime.fromMillisecondsSinceEpoch(data['scanned_at'])
        : null,
      type: MediaType.values.firstWhere(
        (e) => e.name == data['media_type'],
        orElse: () => MediaType.unknown,
      ),
      extension: data['file_extension'],
      
      // 照片元数据
      cameraMake: data['camera_make'],
      cameraModel: data['camera_model'],
      lensMake: data['lens_make'],
      lensModel: data['lens_model'],
      focalLength: data['focal_length']?.toDouble(),
      aperture: data['aperture']?.toDouble(),
      iso: data['iso'],
      shutterSpeed: data['shutter_speed'],
      width: data['width'],
      height: data['height'],
      gpsLatitude: data['gps_latitude']?.toDouble(),
      gpsLongitude: data['gps_longitude']?.toDouble(),
      dateTaken: data['date_taken'] != null 
        ? DateTime.fromMillisecondsSinceEpoch(data['date_taken'])
        : null,
      
      // 视频元数据
      durationSeconds: data['duration_seconds']?.toDouble(),
      frameRate: data['frame_rate']?.toDouble(),
      bitrate: data['bitrate'],
      codec: data['codec'],
    );
  }

  static bool isImageFile(String path) {
    final extension = '.${path.toLowerCase().split('.').last}';
    return [
      '.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.tiff', '.tif',
      '.heic', '.heif', '.raw', '.cr2', '.nef', '.arw', '.dng'
    ].contains(extension);
  }

  static bool isVideoFile(String path) {
    final extension = '.${path.toLowerCase().split('.').last}';
    return [
      '.mp4', '.mov', '.avi', '.mkv', '.webm', '.flv', '.wmv', '.m4v',
      '.3gp', '.mts', '.m2ts', '.ts'
    ].contains(extension);
  }

  static bool isMediaFile(String path) {
    return isImageFile(path) || isVideoFile(path);
  }

  static MediaType getMediaType(String path) {
    if (isImageFile(path)) return MediaType.image;
    if (isVideoFile(path)) return MediaType.video;
    return MediaType.unknown;
  }
}

enum MediaType {
  image,
  video,
  unknown,
}